//lodash中的柯里化 _.curry
const _ = require("lodash");
//01演示 _.curry
// function getSum(a, b, c) {
//     return a + b + c
// }
// const curryed = _.curry(getSum)
// console.log(curryed(1,2,3))
// console.log(curryed(1)(2,3))
// console.log(curryed(1,2)(3))

//02柯里化案例 提取字符串中指定内容
// ''.match(/\s+/g)
// ''.match(/\d+/g)
let match = _.curry(function (reg, str) {
  return str.match(reg);
});
const haveSpace = match(/\s+/g);
// console.log(haveSpace('hello world'))
//查询数组原生中含有空字符的原生
// let filter = _.curry(function(fn,array){
//     return array.filter(fn)
// })
let filter = _.curry((fn, array) => array.filter(fn));
const findSpace = filter(haveSpace);
console.log(findSpace(["cao zy", "cao_zy"]));

//03手写柯里化函数curry
function curry(fn) {
  return function curryFn(...args) {
    //判断实参跟形参的个数
    if (args.length < fn.length) {
      return function () {
        return curryFn(...args, ...arguments);
      };
    }
    return fn(...args);
  };
}
let myFilter = curry((fn, array) => array.filter(fn));
const myFindSpace = myFilter(haveSpace);
console.log(myFindSpace(["cao zy", "cao_zy"]));
